package com.she.iiicoursecenter.activity.tab2_time;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.Toast;

import com.she.iiicoursecenter.iiiApplication;
import com.she.iiicoursecenter.activity.tab1_infomation.Tab1_CourseSingle;
import com.she.iiicoursecenter.activity.template.TemplateActivity;
import com.she.iiicoursecenter.event.base.Event;
import com.she.iiicoursecenter.event.connect.bean.GetContentBean;
import com.she.iiicoursecenter.event.connect.bean.SearchConditionedBean;
import com.she.iiicoursecenter.event.connect.event.SearchConditionedEvent;
import com.she.iiicoursecenter.event.iface.IEventCallback;
import com.she.iiicoursecenter.eventmanager.KenEventManager;
import com.she.iiicoursecenter.view.tab2.Tab2_MainView;

public class Tab2_MainActivity extends TemplateActivity {
	//
	//
	private Tab2_MainView	mMainView;
	private ProgressDialog dialog ;
	
	public Tab2_MainActivity() {
		// TODO Auto-generated constructor stub
	}
	
	private Handler notifyHandler = new Handler(){
		@Override
		public void dispatchMessage(Message msg) {
			// TODO Auto-generated method stub
			if (msg.obj == null) {
				mMainView.notifyAdapter();
				dialog.cancel();
			}else{
				mMainView.notifyAdapter();
				Toast.makeText(Tab2_MainActivity.this, (String)msg.obj, Toast.LENGTH_SHORT).show();
				dialog.cancel();
			}

			super.dispatchMessage(msg);
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		mMainView = new Tab2_MainView(this);
		
		dialog = new ProgressDialog(this);
		 dialog.setMessage("讀取中...");
		 dialog.setIndeterminate(true);
		 dialog.setCancelable(true);
		 dialog.show();
		
		//
		setView(mMainView);
		setContentView(mMainView);	
		getApp().itemListColorCount = -1;
		
		//課程搜尋條件
		SearchConditionedEvent searchConditionedEvent = new SearchConditionedEvent(null);
		searchConditionedEvent.addCallback(new IEventCallback() {
			
			public void result(Event event, Object result) {
				if (((SearchConditionedBean)result).getResult()==0) {
					((iiiApplication)getApplicationContext()).setSearchConditionedData((SearchConditionedBean) result);
					notifyHandler.sendEmptyMessage(0);
				}else{
					((iiiApplication)getApplicationContext()).setSearchConditionedData((SearchConditionedBean) result);
					Message msg = notifyHandler.obtainMessage(0, ((SearchConditionedBean) result).getMessage());
					notifyHandler.sendMessage(msg);
				}
			}
			public void progress(Event event, int progress, String tmpPath) {
			}
		});
		KenEventManager.getInstance().execute(searchConditionedEvent);
	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	@Override
	public void onAttachedToWindow() {
		// TODO Auto-generated method stub
		super.onAttachedToWindow();
	}

	@Override
	public void onDetachedFromWindow() {
		// TODO Auto-generated method stub
		super.onDetachedFromWindow();
	}

}
